#include "gp_beta_server.h"
#include "sock_msg.h"
#include "msg_def.h"
#include "WingCtrlSys.grpc.pb.h"
using GPRPC::BarCodeParam;
using GPRPC::CtrlSysService;
using GPRPC::TakeAPictureParam;
using grpc::ClientContext;



// extern void procPhotoNotify(void);
// extern void procReportMsg(int dataLen, char * dataIn);

// int main(int argc, char** argv) {


//   //initSockServer(1);

//   procPhotoNotify();

//   //while(1)
//   //{
//   //   usleep(100);
//   //}


//   return 0;
// }


// #include <iostream>
// #include <memory>
// #include <string>
// #include <fstream>

// #include <grpcpp/grpcpp.h>

// //#ifdef BAZEL_BUILD
// //#include "examples/protos/helloworld.grpc.pb.h"
// //#else
// //#include "gp.grpc.pb.h"
// //#endif

// using grpc::Channel;
// using grpc::ClientContext;
// using grpc::Status;

//using grpc::ClientReader;
//using grpc::ClientWriter;

// class CtrlSysServiceClient {
//  public:
//   CtrlSysServiceClient(std::shared_ptr<grpc::Channel> channel)
//       : stub_(CtrlSysService::NewStub(channel)) {}

//   CmdResponse TakeAPicture_test(int qpcrIdIn, int isStart) {

//      GPRPC::TakeAPictureParam photoParam;
//      ClientContext context;
//      CmdResponse resp;
//      if(qpcrIdIn==0)
//      {
//         photoParam.set_moduletype(GPRPC::ModuleType::MT_QPCR1);
//      }
//      else if(qpcrIdIn == 1)
//      {
//         photoParam.set_moduletype(GPRPC::ModuleType::MT_QPCR2);
//      }
//      else
//      {
//         resp.set_returnval(ReturnVal::RET_FALSE);
//         return resp;
//      }
     
//      if(isStart)
//      {
//         photoParam.set_switch_(GPRPC::Switch::OPEN);

//      }
//      else
//      {
//         photoParam.set_switch_(GPRPC::Switch::CLOSE);
//      }
//      printf("in TakeAPicture, qpcrid=%d,istart:%d\n",qpcrIdIn,isStart);
     
//      stub_->TakeAPicture(&context, photoParam,&resp);
//      return resp;
//   }

//  private:
//   std::unique_ptr<CtrlSysService::Stub> stub_;
// };

class PMSServiceClient {
 public:
  PMSServiceClient(std::shared_ptr<grpc::Channel> channel)
      : stub_(PMSService::NewStub(channel)) {}

  void OpticChangeChannel_test(void) {

     GPRPC::TakeAPictureParam photoParam;
     ClientContext context;
     CmdResponse resp;
     GPRPC::Channel channelTep;
     stub_->OpticChangeChannel(&context, channelTep,&resp);

  }

 private:
  std::unique_ptr<PMSService::Stub> stub_;
};

int main(int argc, char** argv) {


//CmdResponse reply;
//      std::shared_ptr<::grpc::Channel> channelReport = 
//      grpc::CreateChannel(REPORT_IP_PORT, grpc::InsecureChannelCredentials());
    
      // if(GRPC_CHANNEL_READY != channelReport->GetState(1))
      // {
      //    std::cout<<"create channel for grpc failed"<<std::endl;
      //   return; 
      // } 
     
/*       CtrlSysServiceClient stClientCtl(channelReport); 
      reply = stClientCtl.TakeAPicture_test(0,1);

      if(reply.returnval() == ReturnVal::RET_TRUE)
      {
         std::cout<<"notify take photo success\n"<<std::endl;
      }
      else
      {
         std::cout<<"notify take photo failed\n"<<std::endl;
      } */
     
      //PMSServiceClient stPMSClient(channelReport);
      //stPMSClient.OpticChangeChannel_test();
      initQpcrTempReport();

      initSockServer(1);


      //CtrlSysServiceClient stClientCtl(channelReport); 
/*       reply = stClientCtl.TakeAPicture_test(1,1);

      if(reply.returnval() == ReturnVal::RET_TRUE)
      {
         std::cout<<"notify take photo success\n"<<std::endl;
      }
      else
      {
         std::cout<<"notify take photo failed\n"<<std::endl;
      }
 */
      while(1)
      {
        usleep(1000);
      }

  return 0;
}

// int main(int argc, char** argv) {


// CmdResponse reply;
//       std::shared_ptr<::grpc::Channel> testChannel = 
//       grpc::CreateChannel("10.20.2.11:50069", grpc::InsecureChannelCredentials());
    

//       std::shared_ptr<::grpc::Channel> channelReport = 
//       grpc::CreateChannel(REPORT_IP_PORT, grpc::InsecureChannelCredentials());
    
//       // if(GRPC_CHANNEL_READY != channelReport->GetState(1))
//       // {
//       //    std::cout<<"create channel for grpc failed"<<std::endl;
//       //   return; 
//       // } 
     
//       CtrlSysServiceClient stClientCtl(channelReport); 
//       reply = stClientCtl.TakeAPicture_test(0,1);

//       if(reply.returnval() == ReturnVal::RET_TRUE)
//       {
//          std::cout<<"notify take photo success\n"<<std::endl;
//       }
//       else
//       {
//          std::cout<<"notify take photo failed\n"<<std::endl;
//       }
     
//       PMSServiceClient stPMSClient(testChannel);
//       stPMSClient.OpticChangeChannel_test();

//   return 0;
// }